﻿/*
 * Created by SharpDevelop.
 * User: christophe
 * Date: 7/09/2011
 * Time: 21:02
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Windows;

using mveEngine;

namespace MetaVideoEditorEvolved.UI
{
	/// <summary>
	/// Description of KernelSessionManager.
	/// </summary>
	public class KernelSessionManager : Entity.BaseEntity
	{
		private Kernel realKernel;
		
		private static KernelSessionManager sessionInstance;
		
		protected KernelSessionManager()
		{
			mveEngine.Kernel.SupportedVersionChanged+= new EventHandler(mveEngine_Kernel_SupportedVersionChanged);
		}
		
		public static KernelSessionManager SessionInstance
		{
			get
			{
				if (sessionInstance == null) {
					sessionInstance = new KernelSessionManager();
					sessionInstance.KernelUiEntity = new MetaVideoEditorEvolved.UI.Entity.KernelEntity();
				}
				
				return sessionInstance;
			}
		}
		
		public Kernel RealKernel
		{
			set
			{
				this.realKernel = value;
				// this.realKernel.PropertyChanged+= new PropertyChangedEventHandler(realKernel_PropertyChanged);
				this.MediaItemCollectionChanged();
				this.CacheChanged();
			}
			
			get
			{
				return this.realKernel;
			}
		}
		
		Entity.KernelEntity kernelUiEntity;
		
		public Entity.KernelEntity KernelUiEntity {
			get
			{
				return this.kernelUiEntity;
			}
			
			set
			{
				this.kernelUiEntity = value;
				this.OnNotifyPropertyChanged("KernelUiEntity");
			}
		}
		
		private void realKernel_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			GalaSoft.MvvmLight.Threading.DispatcherHelper.CheckBeginInvokeOnUI(()=>
			                                                                   {
			                                                                   	switch (e.PropertyName) {
			                                                                   		case "ItemCollection" :
			                                                                   			this.MediaItemCollectionChanged();
			                                                                   			break;
			                                                                   		default:
			                                                                   			
			                                                                   			break;
			                                                                   	}
			                                                                   });
		}
		
		private void MediaItemCollectionChanged()
		{
			this.KernelUiEntity.MediaItems.Clear();
			
			foreach (var item in Helper.ConverterHelper.ConvertItemCollection(this.realKernel.ItemCollection)) {
				this.KernelUiEntity.MediaItems.Add(item);
			}
			
			foreach (var item in this.realKernel.ItemCollection.ItemsList) {
				
				if(!string.IsNullOrEmpty(item.Id))
				{
					var foundElement = Helper.ConverterHelper.FindElementInTree(this.KernelUiEntity.MediaItems.ToList(), new Guid(item.Id));
					
					if(foundElement!=null)
					{
						this.KernelUiEntity.FlatListOfMediaItems.Add(foundElement);
					}
				}
			}					
		}

//		private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
//		{
//			mveEngine.Item item = sender as mveEngine.Item;
//			Entity.VideoMediaElement video = null;
//			Entity.SerieMediaItem serie = null;
//			Entity.SeasonMediaItem season = null;
//			Entity.MovieMediaItem movie = null;
//			
//			switch (e.PropertyName) {
//				case "Title" :
//					video = this.RetrieveVideoItem(new Guid(item.Id));
//					if(video!=null)
//					{
//						video.Title = item.Title;
//					}
//					break;
//					
//				case "OriginalTitle":
//					video = this.RetrieveVideoItem(new Guid(item.Id));
//					if(video!=null)
//					{
//						video.OriginalTitle = item.OriginalTitle;
//					}
//					break;
//					
//				case "SortTitle":
//					video = this.RetrieveVideoItem(new Guid(item.Id));
//					if(video!=null)
//					{
//						video.SortTitle = item.SortTitle;
//					}
//					break;
//					
//				case "DateAdded":
//					video = this.RetrieveVideoItem(new Guid(item.Id));
//					if(video!=null)
//					{
//						video.DateAdded = item.DateAdded;
//					}
//					break;
//					
//				case "Year":
//					video = this.RetrieveVideoItem(new Guid(item.Id));
//					if(video!=null)
//					{
//						video.Year = item.Year;
//					}
//					break;
//					
//				case "RunningTime":
//					video = this.RetrieveVideoItem(new Guid(item.Id));
//					if(video!=null)
//					{
//						video.RunningTime = item.RunningTime;
//					}
//					break;
//					
//				case "Rating" :
//					video = this.RetrieveVideoItem(new Guid(item.Id));
//					if(video!=null)
//					{
//						video.Rating = item.Rating;
//					}
//					break;
//					
//				case "MPAARating" :
//					video = this.RetrieveVideoItem(new Guid(item.Id));
//					if(video!=null)
//					{
//						video.MPAARating = item.MPAARating;
//					}
//					break;
//					
//					
//				case "Overview":
//					video = this.RetrieveVideoItem(new Guid(item.Id));
//					if(video!=null)
//					{
//						video.Overview = item.Overview;
//					}
//					break;
//					
//				case "SeriesName":
//					serie = this.RetrieveVideoItem(new Guid(item.Id)) as Entity.SerieMediaItem;
//					if(serie!=null)
//					{
//						serie.SeriesName = item.SeriesName;
//					}
//					break;
//					
//				case "SeasonNumber" :
//					season = this.RetrieveVideoItem(new Guid(item.Id)) as Entity.SeasonMediaItem;
//					if(season!=null)
//					{
//						season.SeasonNumber = item.SeasonNumber;
//					}
//					break;
//					
//				case "PrimaryImage" :
//					video = this.RetrieveVideoItem(new Guid(item.Id));
//					if(video!=null)
//					{
//						video.PrimaryPoster = item.PrimaryImage.ToPosterEntity();
//					}
//					break;
//					
//				case "PrimaryBackdrop":
//					var mediaElement =  this.RetrieveVideoItem(new Guid(item.Id));
//					if(mediaElement!=null)
//					{
//						video = mediaElement as Entity.VideoMediaElement;
//						if (serie!=null) {
//							video.PrimaryBackdrop = item.PrimaryBackdrop.ToPosterEntity();
//						}
//					}
//					
//					break;
//					
//				case "ImagesPaths":
//					video =  this.RetrieveVideoItem(new Guid(item.Id)) as Entity.VideoMediaElement;
//					if(video!=null)
//					{
//						video.Posters.Clear();
//						
//						foreach (var p in item.ImagesPaths) {
//							video.Posters.Add(p.ToPosterEntity());
//						}					
//					}
//					break;
//					
//				case "BackdropImagePaths":
//					video =  this.RetrieveVideoItem(new Guid(item.Id)) as Entity.VideoMediaElement;
//					if(video!=null)
//					{
//						video.Backdrops.Clear();
//						
//						foreach (var p in item.BackdropImagePaths) {
//							video.Backdrops.Add(p.ToPosterEntity());
//						}
//					}
//					break;
//					
//				default:
//					mveEngine.Logger.LoggerInstance.ReportError(string.Format("Property {0} not handle in session manager", e.PropertyName));
//					break;
//			}
//		}		
		
		private Entity.VideoMediaElement RetrieveVideoItem(Guid id)
		{
			var video =this.KernelUiEntity.FlatListOfMediaItems.FirstOrDefault(
				(media)=>
				{
					return media is Entity.VideoMediaElement &&
						((Entity.VideoMediaElement)media).Id.ToString() == id.ToString();
				}) ;
			
			return video as Entity.VideoMediaElement;
		}
		
		private void CacheChanged()
		{
			sessionInstance.KernelUiEntity.Cache = new MetaVideoEditorEvolved.UI.Entity.Cache()
			{
				DetectedVersion = mveEngine.Kernel.DetectedCacheVersion(),
				SupportedVersion = mveEngine.Kernel.SupportedCacheVersion()
			};
		}
		
		private void mveEngine_Kernel_SupportedVersionChanged(object sender, EventArgs e)
		{
			if (this.KernelUiEntity.Cache!=null) {
				this.KernelUiEntity.Cache.SupportedVersion = mveEngine.Kernel.SupportedCacheVersion();
			}
		}
	}
}
